﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AvatarHelper;
using Avatar.Model;

public partial class Management_Article : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                ddl_Category.DataSource = _adc.NewsCategory.ToList();
                ddl_Category.DataTextField = "name";
                ddl_Category.DataValueField = "id";
                ddl_Category.DataBind();
            }
        }
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            if (txt_Content.Value.Length > 4000) throw new Exception("内容不能大于4000个字符");
            if (txt_Title.Text.Length > 100) throw new Exception("内容不能大于100个字符");
            if (string.IsNullOrEmpty(txt_Content.Value)) throw new Exception("内容不能为空");
            if (string.IsNullOrEmpty(txt_Title.Text)) throw new Exception("内容不能为空");

            using (var _adc = DBHelper.GetDataContext())
            {
                News _n = new News();
                _n.ID = Guid.NewGuid();
                _n.Title = txt_Title.Text;
                _n.Content = txt_Content.Value;
                _n.PublishDate = DateTime.Now;
                _n.Hits = 0;
                _n.CategoryID = Convert.ToInt32(ddl_Category.SelectedValue);

                _adc.News.InsertOnSubmit(_n);
                _adc.SubmitChanges();

                txt_Title.Text = "";
                txt_Content.Value = "";
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "添加新闻出错："+_e.Message;
        }
    }
}
